
//登录方法：
export function loginFn({
	props,
	callback
} = {}) {
	/*props 参数
	 props: {store:this.$store,appid:this.appid,secret:this.secret}
	*/
	console.log('登录信息')
	uni.login({
		success: (res) => {
			const {
				code
			} = res;
			if (!code) return;
			const params = {
				js_code: code,
				appid: props.appid,
				secret: props.secret
			};
			props.store.dispatch('user/login', params)
				.then((res) => {
					// if(res && res.userInfo) props.store.dispatch('user/getCustomerList',{token:res.userInfo.token});
					// callback && callback(res);
				});
		}
	});
}

export function codeFormate(type) {
	const list = statusList
	let text = type;
	list.map((v) => {
		if (v.code == type) {
			text = v.value;
		}
	})
	return text;
}

//位置授权：
export function checklocation(props) {
	wx.getSetting({
		success: (res) => {
			// console.log(res,res.authSetting['scope.userLocation'],'res授权=======getSetting=======',res,res.authSetting['scope.userLocation'] == false)

			//'scope.userLocation' 这个属性存在说明之前调起过授权，判断后来关闭了，则提示用户手动去授权
			if (res.authSetting && res.authSetting['scope.userLocation'] == false) {
				// console.log('进来手动授权！！！')
				wx.showModal({
					title: '提示',
					content: '您未授权位置，请点击右上角设置',
					success: (result) => {
						if (result.confirm) {
							wx.openSetting({
								complete: (res) => {
									//todo
								}
							})
						}

					}
				})

			} else {
				//第一次直接打开位置授权
				getLocation(props)
			}
		},
		fail: (res) => {
			// console.log('getSetting--失败', res)
		},
	})

}

export function getLocation(props) {
	wx.getLocation({
		type: 'wgs84',
		success: (res) => {
			let params = {
				OpenID: props.openid,
				Longitude: res.longitude,
				Latitude: res.latitude,
				Speed: res.speed,
				Accuracy: res.accuracy,
				// Altitude: 0, //高度
				VerticalAccuracy: res.verticalAccuracy,
				HorizontalAccuracy: res.horizontalAccuracy
			}
			props.store.dispatch('uni/UplLocation', params);
		}
	});
}
